<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择活动区域">
          <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
      <el-form />
    </el-form></div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编码不能重复自定义验证规则
    const validCode = (rule, value, callback) => {
      // 取出所有的部门编码
      let existCodeList = this.originList.map(item => item.code)
      // 编辑状态下应排除当前的部门编码,再进行判断
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      // 判断输入的value是否已经存在
      existCodeList.includes(value) ? callback(new Error(`部门编码${value}重复`)) : callback()
    }
    // 同级部门名称不能重复
    const validName = (rule, value, callback) => {
      // 先过滤获取所有同级部门信息(pid等于父级的id)
      // 再用map获取他们的name
      // 在name里判断输入的name是否重复
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      // 如果是编辑状态
      if (this.isEdit) {
        // 先通过id找到部门本身
        // 再获取其pid
        // 通过找pid相同的获取兄弟部门,并排除本身
        const obj = this.originList.find(item => item.id === this.parentId)
        const pid = obj.pid
        existNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }
      existNameList.includes(value) ? callback(new Error(`部门名称${value}重复`)) : callback()
    }
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      employees: [], // 负责人列表
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimple()
    this.getDepartDetail()
  },
  methods: {
    // 新增部门
    async addDepartments() {
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      // 给用户提示
      this.$message.success(res.message)
      // 父级关闭弹框
      this.$emit('update-list')
    },

    // 编辑部门
    async updateDepartments() {
      const res = await updateDepartments(this.form)
      // 给用户提示
      this.$message.success(res.message)
      // 父级关闭弹框
      this.$emit('update-list')
    },

    // 点击确定按钮
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.isEdit ? this.updateDepartments() : this.addDepartments()
      })
    },

    // 点击取消按钮
    cancel() {
      this.$emit('close')
    },

    // 获取负责人列表
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employees = res.data
    },

    // 获取部门详情
    async getDepartDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        this.form = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
    width: 100%;
}
.el-form {
    padding: 0 60px 0 20px;
}
</style>
